package com.audaque.springboot.foshanupload.web.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author zgb
 * @desc ...
 * @date 2023-07-19 18:23:15
 */
@Slf4j
@Component
public class DoFilterExceptionFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            // 异常捕获，发送到error controller
            request.setAttribute("doFilterException", e);
            //将异常分发到/error/exthrow控制器
            try {
                request.getRequestDispatcher("/doFilterException").forward(request, response);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {

    }
}
